package com.sport.controller;

import java.sql.Timestamp;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sport.po.Manager;
import com.sport.po.User;
import com.sport.service.AccountService;

@Controller
public class AccountAction {
	
	@Autowired
	private AccountService accountService;
	//登录
	@RequestMapping("/LoginAction.action")
	public String login(String login_name,String password,String role,HttpSession session,HttpServletRequest req){
		String url=req.getHeader("Referer");
		if(role.equalsIgnoreCase("user")){
		
			User user=new User();
			user.setPassword(password);
			user.setUsername(login_name);
			System.out.println(user);
			user=accountService.userLogin(user);
			System.out.println("login:"+user);
			if(user!=null){
				session.setAttribute("user", user);
				if(url!=null){
					System.out.println("url:........"+url);
					return "redirect:"+url;
				}
				return "center/center1.1";
			}
		}else if(role.equalsIgnoreCase("manager")){
			Manager manager=new Manager();
			manager.setLoginname(login_name);
			manager.setPassword(password);
			System.out.println(manager);
			manager=accountService.managerLogin(manager);
			System.out.println("login:"+manager);
			if(manager!=null){
				session.setAttribute("manager", manager);
				return "manager/center1.1";
			}
		}
		return null;
	}
	
	//注册
	@RequestMapping("/ManagerRegister.action")
	public void ManagerRegister(String loginname,String password,String sex,String phone,String realname,String type,String email){
		Manager manager=new Manager();
		manager.setEmail(email);
		manager.setLoginname(loginname);
		manager.setPassword(password);
		manager.setPhone(phone);
		manager.setSex(sex);
		manager.setType(type);
		manager.setRealname(realname);
		int i=accountService.managerRegister(manager);
		System.out.println(i);
		
		
	}
	@RequestMapping("/UserRegister.action")
	public void UserRegister(String username,String password,String realname,String age,String sex,String birthday,String phone,String email ){
		User user=new User();
		int age1=Integer.parseInt(age);
		System.out.println(age);
		Date d=new Date();
		Timestamp time=new Timestamp(d.getTime());
		user.setUsername(username);
		user.setBirthday(birthday);
		user.setEmail(email);
		user.setPassword(password);
		user.setPhone(phone);
		user.setRealname(realname);
		user.setSex(sex);
		user.setAge(age1);
		user.setCreate_time(time);
		System.out.println(user);
		int i=accountService.userRegister(user);
		System.out.println(i);
	}
	
	@RequestMapping("/LoginOut.action")
	public String loginOut(HttpSession session){
		//注销登录
		User user=(User)session.getAttribute("user");
		Manager manager=(Manager)session.getAttribute("manager");
		if(user!=null){
			session.removeAttribute("user");  
		}else if(manager!=null){
			session.removeAttribute("manager");  
		}
		return "home.jsp";
	}
}
